<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房屋名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入房屋名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="租房类型" prop="leaseType">
        <el-select v-model="queryParams.leaseType" placeholder="请选择租房类型" clearable style="width: 100px;">
          <el-option v-for="dict in le_lease_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="房屋户型" prop="houseType">
        <el-select v-model="queryParams.houseType" placeholder="请选择房屋户型" clearable style="width: 100px;">
          <el-option v-for="dict in le_house_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="租赁状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择租赁状态" clearable style="width: 100px;">
          <el-option v-for="dict in le_lease_state" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['lease:leLeaseHouse:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['lease:leLeaseHouse:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['lease:leLeaseHouse:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['lease:leLeaseHouse:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="leLeaseHouseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="租房编号" align="center" prop="leaseHouseId" width="150" />
      <el-table-column label="房屋名称" align="center" prop="name" width="200" />
      <el-table-column label="租房类型" align="center" prop="leaseType" width="90">
        <template #default="scope">
          <dict-tag :options="le_lease_type" :value="scope.row.leaseType" />
        </template>
      </el-table-column>
      <el-table-column label="房屋面积" align="center" prop="area" width="90" />
      <el-table-column label="房屋户型" align="center" prop="houseType" width="90">
        <template #default="scope">
          <dict-tag :options="le_house_type" :value="scope.row.houseType" />
        </template>
      </el-table-column>
      <el-table-column label="所在区域" align="center" prop="district" width="90">
        <template #default="scope">
          <dict-tag :options="le_zone_type" :value="scope.row.district" />
        </template>
      </el-table-column>
      <el-table-column label="租赁价格" align="center" prop="leasePrice" width="90" />
      <el-table-column label="租赁状态" align="center" prop="state" width="90">
        <template #default="scope">
          <dict-tag :options="le_lease_state" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row, scope.index)"
            v-hasPermi="['lease:leLeaseHouse:query']">详细</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['lease:leLeaseHouse:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['lease:leLeaseHouse:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 租房信息详细 -->
    <el-dialog title="租房信息详细" v-model="detail" width="800px" append-to-body>
      <el-form :model="form" label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="租房编号：">{{ form.leaseHouseId }}</el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="房屋名称：">{{ form.name }}</el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="租房类型：">
              <template #default="scope">
                <dict-tag :options="le_lease_type" :value="form.leaseType" />
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="房屋面积：">{{ form.area }} 平方米</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="房屋户型：">
              <template #default="scope">
                <dict-tag :options="le_house_type" :value="form.houseType" />
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所在区域：">{{ form.district }}</el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="详细地址：">{{ form.address }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="租赁价格：">{{ form.leasePrice }}元/月</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="付款方式："> <template #default="scope">
                <dict-tag :options="le_pay_type" :value="form.payType" />
              </template></el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="房屋标签：">{{ form.label }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="房屋优势：">{{ form.advantage }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所在楼层：">{{ form.floor }}层</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最高楼层：">{{ form.maxFloor }}层</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="租赁状态："><template #default="scope">
                <dict-tag :options="le_lease_state" :value="form.state" />
              </template></el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="房屋图片：">
              <template v-for="item in form.mediaPaths">
                &nbsp;&nbsp;<image-preview :src="item" :width="80" :height="80" />
              </template>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="detail = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>


    <!-- 添加或修改租房信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="leLeaseHouseRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="租房类型" prop="leaseType">
          <el-select v-model="form.leaseType" placeholder="请选择租房类型">
            <el-option v-for="dict in le_lease_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房屋名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入房屋名称" />
        </el-form-item>
        <el-form-item label="房屋面积" prop="area">
          <el-input v-model="form.area" placeholder="请输入房屋面积" />
        </el-form-item>
        <el-form-item label="房屋户型" prop="houseType">
          <el-select v-model="form.houseType" placeholder="请选择房屋户型">
            <el-option v-for="dict in le_house_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所在区域" prop="district">
          <el-select v-model="form.district" placeholder="请选择所在区域">
            <el-option v-for="dict in le_zone_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="租赁价格" prop="leasePrice">
          <el-input v-model="form.leasePrice" placeholder="请输入租赁价格" />
        </el-form-item>
        <el-form-item label="付款方式" prop="payType">
          <el-select v-model="form.payType" placeholder="请选择付款方式">
            <el-option v-for="dict in le_pay_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="租赁状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择租赁状态">
            <el-option v-for="dict in le_lease_state" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所在楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入所在楼层" />
        </el-form-item>
        <el-form-item label="最高楼层" prop="maxFloor">
          <el-input v-model="form.maxFloor" placeholder="请输入最高楼层" />
        </el-form-item>
        <el-form-item label="楼层朝向" prop="direction">
          <el-select v-model="form.direction" placeholder="请选楼层朝向">
            <el-option v-for="dict in le_house_dir" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房屋标签" prop="label">
          <el-checkbox-group v-model="form.label">
            <el-checkbox v-for="dict in le_house_label" :key="dict.value" :label="dict.value">
              {{ dict.label }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="房屋优势" prop="advantage">
          <el-select v-model="form.advantage" placeholder="请输入房屋优势">
            <el-option v-for="dict in le_house_advantage" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房屋图片" prop="mediaPath">
          <image-upload v-model="form.mediaPath" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="LeLeaseHouse">
import { listLeLeaseHouse, getLeLeaseHouse, delLeLeaseHouse, addLeLeaseHouse, updateLeLeaseHouse } from "@/api/lease/leLeaseHouse";

const { proxy } = getCurrentInstance();
const { le_house_label, le_house_advantage, le_zone_type, le_lease_state, le_house_type, le_pay_type, le_house_dir, le_lease_type } = proxy.useDict('le_house_label', 'le_house_advantage', 'le_zone_type', 'le_lease_state', 'le_house_type', 'le_pay_type', 'le_house_dir', 'le_lease_type');

const leLeaseHouseList = ref([]);
const open = ref(false);
const detail = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    leaseType: null,
    name: null,
    area: null,
    houseType: null,
    province: null,
    city: null,
    district: null,
    address: null,
    leasePrice: null,
    payType: null,
    label: null,
    state: null,
    floor: null,
    maxFloor: null,
    direction: null,
    advantage: null,
    mediaPath: null,
    accessCount: null,
    mediaPaths: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询租房信息列表 */
function getList() {
  loading.value = true;
  listLeLeaseHouse(queryParams.value).then(response => {
    leLeaseHouseList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  detail.value = false;
  reset();
}

/** 详细按钮操作 */
function handleView(row) {
  detail.value = true;
  form.value = row;
  form.value.mediaPaths = form.value.mediaPath.split(",");
}

// 表单重置
function reset() {
  form.value = {
    leaseHouseId: null,
    leaseType: null,
    name: null,
    area: null,
    houseType: null,
    province: null,
    city: null,
    district: null,
    address: null,
    leasePrice: null,
    payType: null,
    label: [],
    state: null,
    floor: null,
    maxFloor: null,
    direction: null,
    advantage: null,
    mediaPath: null,
    accessCount: null,
    updateBy: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("leLeaseHouseRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}


// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.leaseHouseId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加租房信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _leaseHouseId = row.leaseHouseId || ids.value
  getLeLeaseHouse(_leaseHouseId).then(response => {
    form.value = response.data;
    form.value.label = form.value.label.split(",");
    open.value = true;
    title.value = "修改租房信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["leLeaseHouseRef"].validate(valid => {
    if (valid) {
      form.value.label = form.value.label.join(",");
      if (form.value.leaseHouseId != null) {
        updateLeLeaseHouse(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addLeLeaseHouse(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _leaseHouseIds = row.leaseHouseId || ids.value;
  proxy.$modal.confirm('是否确认删除租房信息编号为"' + _leaseHouseIds + '"的数据项？').then(function () {
    return delLeLeaseHouse(_leaseHouseIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('lease/leLeaseHouse/export', {
    ...queryParams.value
  }, `leLeaseHouse_${new Date().getTime()}.xlsx`)
}

getList();
</script>
